package com.tengju.bff.interfaces.covercharge.util.alarmUtil;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 消息配置
 *
 * @author Giles
 */
@Component
public class MsgSendConfig {

    @Value("${spring.application.name:}")
    private String applicationName;

    @Value("${server.port:}")
    private String serverPort;

    @Value("${spring.profiles.active:default}")
    private String profilesActive;

    @Value("${org.notify.enabled:true}")
    private boolean notifyEnabled;

    @Value("${org.notify.pushCount:10}")
    private int notifyPushCount;

    @Value("${org.notify.pushPoolSize:10}")
    private int notifyPushPoolSize;


    private ThreadPoolTaskExecutor poolTaskExecutor;

    private static MsgSendConfig instance;

    private MsgSendConfig() {
    }

    public static MsgSendConfig getInstance() {
        return MsgSendConfig.instance;
    }

    @PostConstruct
    public void init() {
        setInstance(this);
        initPoolTaskExecutor();
    }

    public void initPoolTaskExecutor() {
        if (poolTaskExecutor != null) {
            return;
        }
        //初始化消息线程池
        poolTaskExecutor = new ThreadPoolTaskExecutor();
        poolTaskExecutor.setBeanName("msg-thread");
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(100);
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(2);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(50);
        //线程池维护线程所允许的空闲时间
        poolTaskExecutor.setKeepAliveSeconds(30000);
        //拒绝执行任务后的操作（抛弃当前的任务）
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());

        poolTaskExecutor.initialize();
    }

    private static void setInstance(MsgSendConfig instance) {
        MsgSendConfig.instance = instance;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public String getProfilesActive() {
        return profilesActive;
    }

    public void setProfilesActive(String profilesActive) {
        this.profilesActive = profilesActive;
    }

    public ThreadPoolTaskExecutor getPoolTaskExecutor() {
        return poolTaskExecutor;
    }

    public boolean isNotifyEnabled() {
        return notifyEnabled;
    }

    public void setNotifyEnabled(boolean notifyEnabled) {
        this.notifyEnabled = notifyEnabled;
    }

    public int getNotifyPushCount() {
        return notifyPushCount;
    }

    public void setNotifyPushCount(int notifyPushCount) {
        this.notifyPushCount = notifyPushCount;
    }

    public String getServerPort() {
        return serverPort;
    }

    public int getNotifyPushPoolSize() {
        return notifyPushPoolSize;
    }

    public void setNotifyPushPoolSize(int notifyPushPoolSize) {
        this.notifyPushPoolSize = notifyPushPoolSize;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }

    public void setPoolTaskExecutor(ThreadPoolTaskExecutor poolTaskExecutor) {
        this.poolTaskExecutor = poolTaskExecutor;
    }

}
